using System.Web.Routing;
using LogikBug.ModularMvc.Routing;

namespace LogikBug.ModularMvc
{
	public static class RouteCollectionExtensions
	{
		public static ModuleRoute MapRoute(this RouteCollection routes, IModule module, string name, string url, object defaults)
		{
			routes.ThrowIfNull("routes");
			name.ThrowIfNull("routes");
			url.ThrowIfNull("routes");

			var route = new ModuleRoute(url)
			{
				Module = module,
				Defaults = new RouteValueDictionary(defaults)
			};

			routes.Add(name, route);
			
			return route;
		}

		public static ModuleRoute MapRoute(this RouteCollection routes, IModule module, string name, string url, object defaults, object constraints)
		{
			routes.ThrowIfNull("routes");
			name.ThrowIfNull("routes");
			url.ThrowIfNull("routes");

			var route = new ModuleRoute(url)
			{
				Module = module,
				Defaults = new RouteValueDictionary(defaults),
				Constraints = new RouteValueDictionary(constraints),
			};

			routes.Add(name, route);

			return route;
		}

		public static ModuleRoute MapRoute(this RouteCollection routes, IModule module, string name, string url, object defaults, string[] namespaces)
		{
			routes.ThrowIfNull("routes");
			name.ThrowIfNull("routes");
			url.ThrowIfNull("routes");

			var route = new ModuleRoute(url)
			{
				Module = module,
				Constraints = new RouteValueDictionary(),
				Defaults = new RouteValueDictionary(defaults),
				DataTokens = new RouteValueDictionary(new { Namespaces = namespaces })
			};

			routes.Add(name, route);

			return route;
		}

		public static ModuleRoute MapRoute(this RouteCollection routes, IModule module, string name, string url, object defaults, object constraints, string[] namespaces)
		{
			routes.ThrowIfNull("routes");
			name.ThrowIfNull("routes");
			url.ThrowIfNull("routes");

			var route = new ModuleRoute(url)
			{
				Module = module,
				Defaults = new RouteValueDictionary(defaults),
				Constraints = new RouteValueDictionary(constraints),
				DataTokens = new RouteValueDictionary(new { Namespaces = namespaces })
			};

			routes.Add(name, route);

			return route;
		}
	}
}